<?php
// +----------------------------------------------------------------------
// | module: 培训管理
// +----------------------------------------------------------------------
// | Author: 小西瓜 <751707632@qq.com>
// +----------------------------------------------------------------------
namespace app\api\controller;

use cmf\controller\BaseController;
use app\api\controller\Common;
use think\Db;
use think\Validate;
use app\api\model\PortalCategoryModel;
use app\api\model\ImsStramingLiveModel;
use app\api\model\ImsAddressModel;
use app\api\model\ImsJoinLogModel;
use app\api\model\SlideItemModel;

class StramingController extends CommonController
{

	/**
	 * [stramingCategoryOne 培训一级分类]
	 * @return [type] [description]
	 */
	public function stramingCategoryOne()
	{
		$param = request()->param();
		if ( isset($param['token_key']) && !empty($param['token_key']) ) $this->verifyToken($param['token_key']); // 刷新token过期时间
		// 获取会议分类
		$categoryModel = new PortalCategoryModel();
		$category = $categoryModel->categoryTree(1,1,'id,name,more');
		foreach ( $category as $key => $val ) {
			$category[$key]['image'] = cmf_get_image_url(str_replace("\\","/",$val['more']['thumbnail']));
			unset($category[$key]['more']);
		}

		return returnJson(200, 'CategoryOne', $category);
	}

	/**
	 * [stramingCategoryInfo 二级分类及内容]
	 * @return [array] [description]
	 */
	public function stramingCategoryInfo()
	{
		$param = request()->param(); // 获取用户传参	
		if ( isset($param['token_key']) && !empty($param['token_key']) ) $this->verifyToken($param['token_key']); // 刷新token过期时间
		// 数据验证
		$rule = [
			'type_id' => 'require|number',
			'page'	=> 'number',
			'limit'	=> 'number',
        ];	
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }

        // 获取分类图片
        $data = array();
        $image = Db::name('portal_category')->where('id', $param['type_id'])->value('more');
        $image = json_decode($image);
        $data['image'] = cmf_get_image_url(str_replace("\\","/",$image->thumbnail));

        // 获取子分类
        $categoryModel = new PortalCategoryModel();
		$category = $categoryModel->categoryTree($param['type_id'],1,'id,name,more');
		foreach ( $category as $key => $val ) {
			$category[$key]['image'] = cmf_get_image_url(str_replace("\\","/",$val['more']['thumbnail']));
			unset($category[$key]['more']);
		}
		$data['category'] = $category;

		// 获取排名靠前的内容
		$typeIdSon = array_column($category, 'id');
		if ( isset($param['page']) && !empty($param['page']) ) $this->page = $param['page'];
		if ( isset($param['limit']) && !empty($param['limit']) ) $this->limit = $param['limit'];
		if ( isset($param['keyword']) && !empty($param['keyword']) ) {
			$where['vm.vm_title'] = ['like', '%'.$param['keyword'].'%']; // 标题查询
		}
		$stramingModel = new ImsStramingLiveModel();
		$where['vm.is_delete'] = 0; // 未删除
		$where['vm.status'] = ['in', '2,4,5']; // 审核通过、培训中、已完成
		$where['vm.is_open'] = 1; // 公开的
		$where['vm.type_id'] = ['in', $typeIdSon];  // 相关分类ID
		$data['straming'] = $stramingModel->stramingList($where, $this->page, $this->limit, 'vm.score_mean desc, vm.id desc');	

        return returnJson(200, 'CategoryInfo',$data);
	}

	/**
	 * [stramingCategoryResource 子分类内容]
	 * @return [type] [description]
	 */
	public function stramingCategoryResource()
	{
		$param = request()->param(); // 获取用户传参
		if ( isset($param['token_key']) && !empty($param['token_key']) ) $this->verifyToken($param['token_key']); // 刷新token过期时间
		// 数据验证
		$rule = [
			'type_id' => 'require|number',
			'msg'	=> 'require|number',
			'page'	=> 'number',
			'limit'	=> 'number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }

        if ( isset($param['page']) && !empty($param['page']) ) $this->page = $param['page'];
		if ( isset($param['limit']) && !empty($param['limit']) ) $this->limit = $param['limit'];
		if ( isset($param['keyword']) && !empty($param['keyword']) ) {
			$where['vm.vm_title'] = ['like', '%'.$param['keyword'].'%']; // 标题查询
		}
		$stramingModel = new ImsStramingLiveModel();
		$where['vm.is_delete'] = 0; // 未删除
		$where['vm.is_open'] = 1; // 公开的
		$where['vm.type_id'] = $param['type_id'];  // 相关分类ID
		$field = 'vm.id,vm.vm_title,vm.cover_pic,vm.player_amount,vm.start_time,vm.share_amount,im.staff_duty,im.staff_name';
		if ( $param['msg'] == 1 ) {	
			$where['vm.status'] = ['in', '2,4']; // 审核通过、培训中
			$data = $stramingModel->stramingList($where, $this->page, $this->limit, 'vm.status desc, vm.score_mean desc, vm.id desc', $field);	
		} else {
			$where['vm.status'] = ['in', '5']; // 已完成
			$data = $stramingModel->stramingList($where, $this->page, $this->limit, 'vm.score_mean desc, vm.id desc', $field);	
		}

        return returnJson(200, 'CategoryResource',$data);
	}

	/**
	 * [stramingUnitStatus 培训状态]
	 * @return [array] [description]
	 */
	public function stramingUnitStatus()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
			'vm_id'	=> 'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
        // 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;
		// 查询培训状态
		$status = Db::name('ims_straming_live')->where('id', $param['vm_id'])->value('status');
		if ( !$status ) return returnJson(400, '培训不存在');
		$data['status'] = $status;
		$data['sttaus_name'] = stramingStatus()[$status];

		return returnJson(200, 'UnitStatus', $data);
	}

	/**
	 * [stramingDatesils 培训详情]
	 * @return [json] [description]
	 */
	public function stramingDatesils()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
			'vm_id'	=> 'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
        // 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;
		// 查询培训状态
		$status = Db::name('ims_straming_live')->where('id', $param['vm_id'])->value('status');
		if ( !$status ) return returnJson(400, '培训不存在');
		// 检查设置
		$verifyControl = $this->verifyControl($param['vm_id']);
		if ( !$verifyControl ) return returnJson(400, '您无法参加该培训');
		// 查询培训详情
		$where['vm.id'] = $param['vm_id'];
        $stramingModel = new ImsStramingLiveModel();
        $stramingInfo = $stramingModel->stramingDateils($where);
        // 判断是培训归属者
        if ( $stramingInfo['user_id'] == $this->user_id ) {
        	$stramingInfo['affiliation'] = 'one';
        } else {
        	$stramingInfo['affiliation'] = 'other';
        }

        return returnJson(200, 'Datesils', $stramingInfo);
	}

	/**
	 * [stramingOnSpotRecord 培训现场参加记录]
	 * @return [type] [description]
	 */
	public function stramingOnSpotRecord()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
			'vm_id'	=> 'require|number',
			'lat'	=> 'require',
			'lng' 	=> 'require',
			'join_status' => 'require|in:1,2'
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
        // 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;
		// 查询培训状态
		$info = Db::name('ims_straming_live')->where('id', $param['vm_id'])->field('user_id, join_way, status, join_address_id, start_time')->find();
		if ( !$info ) return returnJson(400, '培训不存在');
		// 检查设置
		$verifyControl = $this->verifyControl($param['vm_id']);
		if ( !$verifyControl ) return returnJson(400, '您无法参加该培训');
		// 排除培训人签到
		if ( $info['user_id'] == $this->user_id ) return returnJson(400, '你是该培训的主持人，无需签到');
		// 检查参会方式
		if ( !in_array($info['join_way'], [1,3]) ) return returnJson(400, '该培训不支持现场参加');
		// 检查参会地址是否存在以及参会区域
		if ( $address = Db::name('ims_address')->where('id', $info['join_address_id'])->where('status', 1)->field('lat, lng')->find() ) {
			$distance = $this->GetDistance($param['lat'], $param['lng'], $address['lat'], $address['lng']); // 获取定位距离
			if ( $distance > 300 )  return returnJson(400, '您不在会议现场区域');
		} else {
			return returnJson(400, '参会地址不存在或不可用，请联系相关负责人');
		}
		// 检查培训时间
		$start_time = strtotime($info['start_time']);
		if ( ($start_time-time()) > 3600 ) return returnJson(400, '培训开始前一个小时入场');
		if ( $info['status'] == 5 ) return returnJson(400, '会议已经结束');
		// 入场、离场
		$joinLogModel = new ImsJoinLogModel();
		if ( $joinLog = Db::name('ims_join_log')->where('vm_id', $param['vm_id'])->where('join_uid', $this->user_id)->where('join_way', 1)->where('is_vm', 1)->find() ) {
			if ( $param['join_status'] == 1 ) {
				return returnJson(400, '您以登记入场，无需重复登记');
			} else {
				if ( $joinLog['quit_time'] ) return returnJson(400, '您已离场，无需重复离场');
				$data['id'] = $joinLog['id'];
				if ( $joinLogModel->set($data) ) return returnJson(200, '离场成功');
			}
		} else {
			if ( $param['join_status'] == 1 ) {
				$data['vm_id'] = $param['vm_id'];
				$data['join_uid'] = $this->user_id;
				$data['join_way'] = 1;
				$data['is_vm'] = 1;
				if ( $joinLogModel->set($data) ) return returnJson(200, '入场成功');
			} else {
				return returnJson(400, '无入场记录');
			}
		}
	}

	/**
	 * [stramingLive 培训直播]
	 * @return [type] [description]
	 */
	public function stramingLive()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
			'vm_id'	=> 'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
        // 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;
		// 查询培训状态
		$info = Db::name('ims_straming_live')->where('id', $param['vm_id'])->field('user_id, join_way, status, join_address_id, start_time')->find();
		if ( !$info ) return returnJson(400, '培训不存在');
		if ( $info['status'] == 5 ) return returnJson(400, '培训已经结束');
		// 检查设置
		$verifyControl = $this->verifyControl($param['vm_id']);
		if ( !$verifyControl ) return returnJson(400, '您无法参加该培训');
		if ( $info['join_way'] == 1 ) return returnJson(400, '现场培训，无直播');
		// 检查培训时间
		$start_time = strtotime($info['start_time']);
		if ( ($start_time-time()) > 600 ) return returnJson(400, '未到培训时间');
		 
		// 查询培训详情
		$where['vm.id'] = $param['vm_id'];
        $stramingModel = new ImsStramingLiveModel();
        $stramingInfo = $stramingModel->meetingLive($where);

        // 判断是培训归属者
        if ( $info['user_id'] == $this->user_id ) {
        	$stramingInfo['affiliation'] = 'one';
        	// 生成推流地址
        	$push = pushDomain();
        	$stramingInfo['push_domain']['rtmp'] = 'rtmp://'.$this->PrivateKeyA($push['domain'], '', $push['key'], '/straming/'.$stramingInfo['id']);
        } else {
        	$stramingInfo['affiliation'] = 'other';
        	// 生成播流地址
        	$pull = pullDomain();
        	$stramingInfo['pull_domain']['rtmp'] = 'rtmp://'.$this->PrivateKeyA($pull['domain'], '', $pull['key'], '/straming/'.$stramingInfo['id']);
        	$stramingInfo['pull_domain']['flv'] = 'http://'.$this->PrivateKeyA($pull['domain'], '',  $pull['key'], '/straming/'.$stramingInfo['id'].'.flv');
        	$stramingInfo['pull_domain']['m3u8'] = 'http://'.$this->PrivateKeyA($pull['domain'], '', $pull['key'], '/straming/'.$stramingInfo['id'].'m3u8');
        }
		
		return returnJson(200, 'Resource', $stramingInfo);
	}

	/**
	 * [stramingLiveScore 直播评分]
	 * @return [type] [description]
	 */
	public function stramingLiveScore()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
			'vm_id'	=> 'require|number',
			'score' => 'require|between:1,5',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
        // 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;
		// 检查不能给自己评分
		$vmUserId = DB::name('ims_straming_live')->where('id', $param['vm_id'])->value('user_id');
		if ( $vmUserId == $this->user_id )return returnJson(400, '不能自己评分');
		// 检查用户是否观看
		if ( !(Db::name('ims_join_log')->where('vm_id', $param['vm_id'])->where('join_uid', $this->user_id)->where('is_vm', 1)->find()) ) return returnJson(400, '想评分先观看');
		// 检查是否已经评分
		if ( Db::name('ims_live_score')->where('vm_id', $param['vm_id'])->where('user_id', $this->user_id)->where('is_vm', 1)->find() ) return returnJson(400, '不能重复评分');

		// 添加评分
		$res = $this->GetScore($param['vm_id'], $param['score'], 1, $vmUserId);
		if ( $res ) {
			return returnJson(200, '评分成功');
		} else {
			return returnJson(200, '评分失败');
		}
	}

	/**
	 * [verifyControl 培训设置检查]
	 * @param [int] $[vm_id] [<培训ID>]
	 * @return [bool] [description]
	 */
	private function verifyControl($vm_id)
	{
		$control = Db::name('ims_straming_live')->where('id', $vm_id)->field('user_id, control')->find();
		// 用户自己查看
		if ( $control['user_id'] == $this->user_id ) return true;
		// 检查设置
		if ( $control['control'] == 1 ) {
			// 公开
			return true;
		} elseif ( $control['control'] == 2 ) {
			// 谁可以看
			$info = DB::name('ims_straming_restricted')->where('vm_id', $vm_id)->where('rule', 1)->field('restricted_id')->select()->toArray();
			$restricted_id = array_column($info, 'restricted_id');
			if ( in_array($this->user_id, $restricted_id) ) {
				return true;
			} else {
				return false;
			}
		} elseif ( $control['control'] == 3 ) {
			// 谁不可以看
			$info = DB::name('ims_straming_restricted')->where('vm_id', $vm_id)->where('rule', 2)->field('restricted_id')->select()->toArray();
			$restricted_id = array_column($info, 'restricted_id');
			if ( in_array($this->user_id, $restricted_id) ) {
				return false;
			} else {
				return true;
			}
		}
		return false; // 未知状态
	}

	/**
	 * [stramingLivePlayback 培训直播回放]
	 * @return [json] [description]
	 */
	public function stramingLivePlayback()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
			'vm_id'	=> 'require|number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
        // 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;
		// 查询培训状态
		$info = Db::name('ims_straming_live')->where('id', $param['vm_id'])->field('user_id, join_way, status, store_link, is_open, join_address_id, start_time')->find();
		if ( !$info ) return returnJson(400, '培训不存在');
		// 检查设置
		// $verifyControl = $this->verifyControl($param['vm_id']);
		// if ( !$verifyControl ) return returnJson(400, '您无法参加该培训');
		if ( $info['join_way'] == 1 ) return returnJson(400, '现场培训，无直播');
		if ( $info['status'] != 5 ) return returnJson(400, '培训未结束,无回放');
		if ( empty($info['store_link']) || $info['is_open'] == 0 ) return returnJson(400, '无回放或回放未发布');

		// 查询培训详情
		$where['vm.id'] = $param['vm_id'];
        $stramingModel = new ImsStramingLiveModel();
        $stramingInfo = $stramingModel->stramingLivePlayback($where);

        return returnJson(200, 'stramingLivePlayback', $stramingInfo);
	}

	/*******************************************************************/

	/**
	 * 培训申请
	 */
	public function stramingApply()

 	{
		if ( request()->isPost() ) {
			$param = request()->param(); // 获取用户传参		
			// 数据验证
			$rule = [
				'token_key' => 'require',
                'vm_title' 	=> 'require|chsDash',
                'type_id'	=> 'require|number',
                'join_way'	=> 'require|in:1,2,3',
                'join_address_id'	=> 'number',
                'start_time'	=> 'require|date',
                'duration'	=> 'require|number',
                'control'	=> 'require|number',
                'fight_id'	=> 'number'
            ];	
            $validate = new Validate($rule);
            if(!$validate->check($param)){
                return returnJson(400, $validate->getError());
            }
            // 检查登录
			$verifyToken = $this->verifyToken($param['token_key']);
			if ( $verifyToken != '1111') return $verifyToken;
			// 职务权限验证
			$verifyDuty = $this->verifyDuty('straming');
			if ( $verifyDuty != '1111') return $verifyDuty;
			// 检查标题名防止重复
			if ( Db::name('ims_straming_live')->where('vm_title',$param['vm_title'])->find() ) {
				return returnJson(400, '主题已被使用，请更换');
			}

			$data['user_id'] 	= $this->user_id;
			$data['vm_title'] 	= htmlentities(trim($param['vm_title']));
			$data['type_id']	= htmlentities($param['type_id']);
			$data['start_time']		= htmlentities($param['start_time']);
			$data['duration']		= htmlentities($param['duration']);
			$data['control']		= htmlentities($param['control']);
			$data['join_way']		= htmlentities($param['join_way']);

			// 检查分类ID
			if ( !(Db::name('portal_category')->where('id',$data['type_id'])->value('id')) ) return returnJson(400, '分类ID不存在');

			// 检查参加培训方式
			if ( in_array($data['join_way'], [1,3]) ) {
				if ( !isset($param['join_address_id']) || empty($param['join_address_id']) ) return returnJson(400, '现场地址不能为空');
				$data['join_address_id'] = htmlentities($param['join_address_id']);
				// 检查地址
				if ( !(Db::name('ims_address')->where('id',$data['join_address_id'])->value('id')) ) return returnJson(400, '现场地址ID不存在');
			}

			// 检查开始时间
			if ( $data['start_time'] < date("Y-m-d H:i:s", time()) ) return returnJson(400, '开始时间不能晚于当前时间');

			// 处理必须参加人员
			if ( isset($param['join_uid_must']) && !empty($param['join_uid_must']) ) {
				$joinUidMust = explode(",", $param['join_uid_must']);
				// 根据员工工号查出员工ID
				$uid = Db::name('ims_member')->where('staff_number','in',$joinUidAll)->column('staff_number, id');
				$join = array();
				foreach ( $joinUidMust as $k => $v ) {
					// 数据不同步，及时请求保存
					if ( !isset($uid[$v]) || $uid[$v] == '' || $uid[$v] == null ) {
						// 获取员工资料
						$uid[$v] = $this->userInfoOne($v);
					}
					if ($uid[$v]) $join[$k]['join_id'] = $uid[$v];
				}
				$data['join'] = $join;
			}

			// 对战处理
			$fightUser = '';
			if (  isset($param['challenge_id']) && isset($param['fight_id']) ) return returnJson(400, '被挑战人ID与对战ID不能同时存在');
			if ( isset($param['fight_id']) ) {
				if ( !($fightUser = Db::name('ims_straming_live')->where('id',$param['fight_id'])->field('user_id, challenge_id')->find()) ) return returnJson(400, '对战ID不存在');
				if ( $fightUser['challenge_id'] == $this->user_id ) {
					$data['fight_id'] = $param['fight_id']; // 对战ID
					$data['challenge_id'] = $fightUser['user_id']; // 被挑战人ID
				} else {
					return returnJson(400, '对战ID信息有误');
				}
			}
			if ( $data['challenge_id'] ) $challengeInfo = Db::name('ims_member')->where('id',$data['challenge_id'])->field('staff_name, staff_duty')->find();
			$data['challenge_name'] = $challengeInfo['staff_name'];
			if ( isset($param['challenge_id']) ){
				$data['challenge_id']		= $this->userInfoOne($param['challenge_id']); // 获取被挑战员工最新信息	
				if ( empty($data['challenge_id']) || $data['challenge_id'] == 0 ) return returnJson(400, '对战人不存在');
				if ( $data['challenge_id'] ==  $this->user_id ) return returnJson(400, '不能自己挑战自己');
				// 被条挑战人职务权限验证
				$challengeVerifyDuty = $this->verifyDuty('straming', $challengeInfo['staff_duty']);
				if ( $challengeVerifyDuty != '1111') return returnJson(400, '被挑战人无发起培训权限');
				if ( Db::name('ims_straming_game')->where('initiator_uid',$data['user_id'])->where('party_uid',$data['challenge_id'])->where('pk_status', 'in', [0,1])->find() ) return returnJson(400, '您与被挑战人还有未完成的PK!');
				if ( (Db::name('ims_straming_game')->where('initiator_uid',$data['user_id'])->where('pk_status', 'in', [0,1])->count()) > 5 ) return returnJson(400, '您发起的挑战过多，请先处理未完成的PK!');
			}

			// 处理图片
			$file = request()->file('covar_pic'); // 获取图片对象
		    if($file){
		        $info = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif,svg,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads'); // 文件目录
		        if($info){
		            $data['cover_pic'] = '/uploads/'.$info->getSaveName(); // 文件路径
		        }else{
		        	return returnJson(400, $file->getError());
		        }
		    } else {
		    	return returnJson(400, '请上传图片');
		    }
			$data['intro']		=	htmlentities($param['intro']);
			unset($info); // 释放资源

			// 当用户选择部分人可见
			if ( !isset($param['challenge_id']) && !isset($param['fight_id']) && ($param['control'] == 2 || $param['control'] == 3) ) {
				if ( empty($param['restricted_id']) ) return returnJson(400, '受限制人不能为空');
				$restricted_id = explode(",", $param['restricted_id']);
				// 根据员工工号查出员工ID
				$uid = Db::name('ims_member')->where('staff_number','in',$restricted_id)->column('staff_number, id');
				// 全部参会人员
				$restricted = array();
				foreach ( $restricted_id as $k => $v ) {
					// 数据不同步，及时请求保存
					if ( !isset($uid[$v]) || $uid[$v] == '' || $uid[$v] == null ) {
						// 获取员工资料
						$uid[$v] = $this->userInfoOne($v);
					}
					$restricted[$k]['restricted_id'] = $uid[$v];
				}
				$data['restricted'] = $restricted;	
			} else {
				$data['control']	= 1; // 当挑战者ID、对战ID存在设置为公开
			}

			// 实例model添加数据
			$stramingModel = new ImsStramingLiveModel();
			$res = $stramingModel->set($data);

			// $this->traceLog('请求了[发起培训]接口'); // 操作日志
			if ( $res['code'] == 1 ) {
				return returnJson(200, '培训发起成功');
			} else {
				$this->delFile('./uploads/', '.'.$data['cover_pic']);// 删除文件
				return returnJson(400, '培训发起失败');
			}
		} else {
			return returnJson(400, '请使用POST请求方式');
		}
	}

	/**
	 * 培训申请前请求参数
	 */
	public function stramingResource()
	{
		$param = request()->param(); // 获取用户传参
		// 数据验证
		$rule = [
			'token_key' => 'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }
		// 检查登录
		$verifyToken = $this->verifyToken($param['token_key']);
		if ( $verifyToken != '1111') return $verifyToken;

		// 获取培训分类
		$categoryModel = new PortalCategoryModel();
		$data['category'] = $categoryModel->categoryTree(1,2,'id,name');

		// 获取会议地址
		$addressModel = new ImsAddressModel();
		$data['address'] = $addressModel->lists(['status'=>1], '', 'is_default desc');

		$this->traceLog('请求了[培训申请前参数请求]接口'); // 操作日志
		return returnJson(200, 'Resource', $data);
	}

	/*******************************************************************/

	/**
	 * [stramingPkHome 高手擂台-首页]
	 * @return [type] [description]
	 */
	public function stramingPkHome()
	{
		$param = request()->param();
		if ( isset($param['token_key']) && !empty($param['token_key']) ) $this->verifyToken($param['token_key']); // 刷新token过期时间
		// 数据验证
		$rule = [
			'page' => 'number',
            'limit' 	=> 'number',
        ];	
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return returnJson(400, $validate->getError());
        }

		// 获取会议展厅banner
		$slideModel = new SlideItemModel();
		$banner  = $slideModel->show(6);

		$where = [];
		if ( isset($param['page']) && !empty($param['page']) ) $this->page = $param['page'];
		if ( isset($param['limit']) && !empty($param['limit']) ) $this->limit = $param['limit'];
		if ( isset($param['keyword']) && !empty($param['keyword']) ) {
			$where['vm.vm_title'] = ['like', '%'.$param['keyword'].'%']; // 标题查询
		}

		$where['labels'] = 1;
		// 获取经典赛事
		$stramingModel = new ImsStramingLiveModel();
		$pkList = $stramingModel->stramingPkClassic($where, $this->page, $this->limit);

		return returnJson(200, 'stramingPkHome',$pkList);
	}
}
